/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.platform.components.tabs.tabpanel.components;

import it.blueocean.acanto.model.MenuItem;
import it.blueocean.acanto.web.platform.components.panel.AcantoPanel;

import java.lang.reflect.Constructor;

import org.apache.log4j.Logger;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.security.extensions.markup.html.tabs.ISecureTab;

public class DynamicButtons implements ISecureTab, Comparable<Object>
{
	private static final long serialVersionUID = -7014886832920925443L;
	
	private static final Logger logger = Logger.getLogger(DynamicButtons.class);

	private String menuTitle;

	private String tabId;

	private int position;
	
	private AcantoPanel classForSecurityInstance;
	
	public DynamicButtons(MenuItem menuItem)
	{
		setTabId(menuItem.getId());
		setMenuTitle(menuItem.getTitle());
		setPosition(menuItem.getPosition());

		initContentPanel(menuItem.getId());
	}
	
	private void initContentPanel(String tabId)
	{
		try
		{
			Class<?> c = Class.forName(getTabId());
			Constructor<?> constructor = c.getConstructor(new Class[] { String.class });

			setContentPanelForSecurity((AcantoPanel) constructor.newInstance(tabId));
		}
		catch (Exception e)
		{
			logger.error("Unable to instantiate a custom panel class for DynamicButton", e);
		}
	}
	
	private void setContentPanelForSecurity(AcantoPanel thisInstance)
	{
		this.classForSecurityInstance = thisInstance;
	}
	
	private AcantoPanel getClassForSecurityInstance()
	{
		return this.classForSecurityInstance;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Class<AcantoPanel> getPanel()
	{
		return (Class<AcantoPanel>) getClassForSecurityInstance().getClass();
	}

	@Override
	public AcantoPanel getPanel(String panelId)
	{
		initContentPanel(panelId);

		return getClassForSecurityInstance();
	}

	@Override
	public IModel<String> getTitle()
	{
		return new Model<String>(getMenuTitle());
	}

	@Override
	public int compareTo(Object obj)
	{
		int result = this.getPosition() - ((DynamicButtons) obj).getPosition();

		if(result < 0)
			return -1;
		else if(result == 0)
			return 0;
		else
			return 1;
	}

	public String getMenuTitle()
	{
		return menuTitle;
	}

	public void setMenuTitle(String menuTitle)
	{
		this.menuTitle = menuTitle;
	}

	public String getTabId()
	{
		return tabId;
	}

	public void setTabId(String tabId)
	{
		this.tabId = tabId;
	}

	public int getPosition()
	{
		return position;
	}

	public void setPosition(int position)
	{
		this.position = position;
	}

	@Override
	public boolean isVisible()
	{
		// TODO Auto-generated method stub
		return false;
	}

}
